package hl.com.apkcollect;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;

import java.util.Collections;
import java.util.List;
import java.util.Map;


public class MainActivity extends ActionBarActivity {

    private WebView webview;
    protected static final String ACTIVITY_TAG="apkcollect";
    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        //setContentView(R.layout.activity_main);
        webview=new WebView(this);
        webview.getSettings().setJavaScriptEnabled(true);
        OpenApkClass openApkClass=new OpenApkClass();
        try {
            webview.loadUrl("file:///android_asset/html/index.html");
            webview.addJavascriptInterface(openApkClass,"openApkClass");
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        setContentView(webview);
    }

    private class OpenApkClass {
        private List<Map<String, Object>> list = null;
        private PackageManager mPackageManager;
        private List<ResolveInfo> mAllApps;
        public void openAnyvEdu() {
            try{
                Log.i(MainActivity.ACTIVITY_TAG, "------------openAnyvEdu--------------");
                Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
                mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                mPackageManager = mContext.getPackageManager();
                mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
                Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
                String pkg="";
                String cls="";
                String targetPkg="";
                String targetCls="";
                ComponentName componet=null;
                for(ResolveInfo res : mAllApps)
                {
                    pkg = res.activityInfo.packageName;
                    cls = res.activityInfo.name;
                    if(pkg.contains("anyv.edu"))
                    {
                        Log.i(MainActivity.ACTIVITY_TAG, "---------findApk:"+pkg+" and findCls:"+cls+"--------");
                        targetPkg=pkg;
                        targetCls=cls;
                    }
                }
                if(targetPkg.length()>0)
                {
                    componet = new ComponentName(targetPkg, targetCls);
                    Intent intent = new Intent();
                    intent.setComponent(componet);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
                else
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                    builder.setMessage("是否下载？");
                    builder.setTitle("提示");
                    builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            webview.loadUrl("http://114.215.192.127:9090/anyvschool/tempfile/Anyvedu.apk");
                        }
                    });
                    builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.create().show();
                }
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if ( id == R.id.action_settings ) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
